package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.company.Company;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

//合同
@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {
    @Autowired
    private ContractService contractService;
    /**
     * 分页查询合同
     * 1）方法的访问路径 http://localhost:8080/cargo/contract/list.do
     * 2）方法的参数
     * 3）方法的返回内容：
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int pageNum,
                       @RequestParam(defaultValue = "5") int pageSize) {
        // 获取企业id
        String id=getLoginCompanyId();

        //1.调用业务获取数据
        PageInfo pageInfo = contractService.findByPageId(pageNum,pageSize,id);

        //2.存入request域中，提供给页面显示
        request.setAttribute("pageInfo", pageInfo);

        return "cargo/contract/contract-list";
    }

    /**
     * 添加
     * 1）方法的访问路径  http://localhost:8080/cargo/contract/toAdd.do
     * 2）方法的参数
     * 3）方法的返回内容：contract-update.jsp
     */
    @RequestMapping("/toAdd")
    public String toAdd() {

        return "cargo/contract/contract-update";
    }

    /**
     * 添加
     * 1）方法的访问路径  http://localhost:8080/cargo/contract/edit.do
     * 2）方法的参数
     * 3）方法的返回内容：
     */
    @RequestMapping("/edit")
    public String edit(Contract contract) {
        String id=getLoginCompanyId();
        String name=getLoginCompanyName();
        contract.setCompanyId(id);
        contract.setCompanyName(name);

        if (contract.getId() == null || contract.getId().equals("")) {
            //alt+enter
            contractService.save(contract);
        } else {
            //2.判断如果存在ID，代表修改
            contractService.update(contract);
        }
        return "redirect:/cargo/list.do";
    }

}
